#include "Oak.h"

using namespace DirectX;

void Oak::Initialize(HWND WindowHandle, HINSTANCE hInstance, ID3D11Device* Device, ID3D11DeviceContext* DeviceContext)
{
	MainWindowHandle = WindowHandle;
	Graphics.Initialize(WindowHandle);
	Input.Initialize(WindowHandle, hInstance);
	Network.Initialize();
	Sound.Initialize();
	SceneGraph.Initialize();
	Camera.Initialize();
	
	Scene.SetCamera(&Camera);
	Scene.Initialize(0,0, Graphics.GetDevice(), Graphics.GetDeviceContext());

	Scene.LoadScene2("ModelExporter9.dae");

	Sound.LoadAudio("Loopdeloop.wav");
	if(FAILED(Sound.Play("Loopdeloop.wav", XAUDIO2_LOOP_INFINITE, (float)0.2)))
	{
		MessageBox(NULL, "could not play Loopdeloop.wav", NULL, NULL);
	}
};

void Oak::Update(int DeltaTime)
{

	LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
	LARGE_INTEGER Frequency;

	QueryPerformanceFrequency(&Frequency); 
	QueryPerformanceCounter(&StartingTime);

	Input.Update();
	Camera.Update(DeltaTime);

	if (Input.KeyDown(DIK_ESCAPE))
	{
		PostQuitMessage(0);
	}
	if (Input.KeyDown(DIK_LEFT))
	{
		Camera.MoveLeft();

	}
	if (Input.KeyDown(DIK_RIGHT))
	{
		Camera.MoveRight();
	}
	if (Input.KeyDown(DIK_UP))
	{
		Camera.MoveForward();
	}
	if (Input.KeyDown(DIK_DOWN))
	{
		Camera.MoveBackwards();
	}
	if (Input.MouseMove().x > 0)
	{
		Camera.RotateLeft();
	}
	if (Input.MouseMove().x < 0)
	{
		Camera.RotateRight();
	}
	if (Input.MouseMove().y != 0)
	{
		
	}

	Render();

	QueryPerformanceCounter(&EndingTime);
	ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
	ElapsedMicroseconds.QuadPart *= 1000000;
	ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
	DeltaTime = ElapsedMicroseconds.QuadPart;

	FPS = 1 / (ElapsedMicroseconds.LowPart/1000000.0f);

};

void Oak::Render()
{
	Graphics.Render();
	Graphics.RenderText(std::to_string(FPS), 0, 0);
	Scene.Render();
	Graphics.Present();
}
	
void Oak::Shutdown()
{
	Camera.Shutdown();
	Input.Shutdown();
	Sound.Shutdown();
	Graphics.Shutdown();
	Scene.Shutdown();
};
